﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Xml;

namespace doDesktop
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Login button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLogin_Click(object sender, EventArgs e)
        {
            if(textBoxUser.Text != "" && TextBoxPassword.Text != "")
            {
                labelResponse.Text = "";
                try
                {
                    Mandoo.DoAPI mandoo = new Mandoo.DoAPI();
                    if (mandoo.Login(textBoxUser.Text, TextBoxPassword.Text))
                    {
                        XmlDocument xmlDocument = new XmlDocument();
                        xmlDocument.InnerXml = mandoo.Output;
                        XmlNode xmlNode = xmlDocument.SelectSingleNode("/nodes/node");
                        int accointId = int.Parse(xmlNode.SelectSingleNode("accountid").InnerText);
                        if (accointId > 0)
                        {
                            textBoxUser.Text = "";
                            TextBoxPassword.Text = "";
                            this.Hide();
                            new FormSMS(xmlNode).ShowDialog();
                            this.Dispose();
                            this.Close();
                        }
                        else
                        {
                            labelResponse.Text = Resource_ES.loginfaileduserorpass;
                        }
                    }
                    else 
                    {
                        labelResponse.Text = Resource_ES.loginfaileduserorpass;
                    }
                }
                catch
                {
                    labelResponse.Text = Resource_ES.loginfaileduserorpass;
                }
            }
            else
            {
                labelResponse.Text = Resource_ES.loginfaileduserorpass;
            }
        }
       /// <summary>
       /// Form Load
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void Login_Load(object sender, EventArgs e)
        {
            labelResponse.Text = "";
            textBoxUser.Text ="";
            TextBoxPassword.Text = "";
            //buttonLogin.Text = Resource_ES.login;

        }
        /// <summary>
        /// Close of login form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Dispose();
            this.Close();
        }
        /// <summary>
        /// Click Mandoo Logo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBoxLogo_Click(object sender, EventArgs e)
        {
            Process.Start(Resource_ES.mandoourl);
        }
        /// <summary>
        /// Click on create new account
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linkLabelSignup_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start(Resource_ES.mandoosignupurl);
        }

        /// <summary>
        /// If the users press enter on the login textboxes the app try to login
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxUser_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13) 
            { 
                buttonLogin_Click(sender, null);
            
            }
        }
        /// <summary>
        /// Enter on textbox password
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBoxPassword_KeyUp(object sender, KeyEventArgs e)
        {
            textBoxUser_KeyUp(sender, e);
        }

        private void panelContainer_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
